package com.ithiem.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.dubbo.api.SummaryApi;
import com.itheima.model.domain.Summary;
import com.ithiem.dubbo.mappers.SummaryMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SummaryApiImpl implements SummaryApi {
    @Autowired
    private SummaryMapper summaryMapper;
    //查询对应日期的summary记录
    @Override
    public Summary queryByDate(String yestoday) {
        QueryWrapper<Summary> qw = new QueryWrapper<>();
        qw.eq("created_date",yestoday);
        return summaryMapper.selectOne(qw);
    }

    @Override
    public void save(Summary newSummary) {
        //先查询是否有相同的记录(根据日期查),
        QueryWrapper<Summary> qw = new QueryWrapper<>();
        qw.eq("created_date",newSummary.getCreatedDate());
        Summary summary = summaryMapper.selectOne(qw);
        if (summary==null){
            summaryMapper.insert(newSummary);
        }else {
            // 如果有就更新,没有就保存
            newSummary.setId(summary.getId());
            summaryMapper.updateById(newSummary);
        }
    }
}
